﻿package net.sealuitk.containers 
{
	import net.sealuitk.Container;
	import net.sealuitk.gadgets.Grabbar;
	import net.sealuitk.styles.StyleContainer;
	import net.sealuitk.Util;
	/**
	 * ...
	 * @author mkn
	 */
	public class Window extends Container
	{
		protected var grabbar:Grabbar;
		protected var hasGrabbar:Boolean = false;
		
		protected var inheritStyle:Boolean;
		
		// ======== Constructor ========
		
		public function Window(Name:String, Width:int, Height:int, InheritStyle:Boolean = true) {
			super(Name, Width, Height);
			inheritStyle = InheritStyle;
			update();
		}
		
		// ======== Overrides ========
		
		override public function makeChild(ofContainer:Container, IsFloating:Boolean = false):void {
			super.makeChild(ofContainer, IsFloating);
			if (inheritStyle) {
				setStyle(parentContainer.style);
				updateFromStyle();
			}
		}
		
		override public function setStyle(newStyle:StyleContainer):void {
			super.setStyle(newStyle);
			grabbar = newStyle.grabbar;
		}
		
		override public function update():void {
			super.update();
			// TODO: Loopar döds?? 
			//updateGrabbar();
		}
		
		override public function updateFromStyle():void {
			super.updateFromStyle();
			setGrabbar(style.grabbar);
			addChild(grabbar);
		}
		
		override public function updateMask():void {
			if(grabbar != null){
				mask = Util.buildMask(0, 0, width, size.y + grabbar.height);
				if (contains(mask) == false) addChild(mask);
			} else super.updateMask();
		}
		
		// ======== Grabbar ========
		
		public function setGrabbar(newGrabbar:Grabbar):void {
			grabbar = newGrabbar;
			grabbar.makeChild(this);
			
			update();
		}
		
		public function updateGrabbar():void {
			if (grabbar != null) {
				grabbar.width = size.x;
				
				framesLayer.y = grabbar.height;
				fixedLayer.y = grabbar.height;
				floatingLayer.y = grabbar.height;
				background.y = grabbar.height;
				
				hasGrabbar = true;

				updateMask();
				
			} else {
				framesLayer.y = 0;
				floatingLayer.y = 0;
				fixedLayer.y = 0;
				background.y = 0;
				
				hasGrabbar = false;
			}
		}
		
	}

}